/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦，本文采用木兰宽松许可证第2版]
 * 
 * https://zhiqim.org/project/zhiqim_kit/zhiqim_git.htm
 *
 * ZhiqimGit is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.git;

import java.io.File;
import java.io.IOException;

import org.zhiqim.kernel.Global;
import org.zhiqim.kernel.Servicer;
import org.zhiqim.kernel.extend.HashMapSV;
import org.zhiqim.kernel.logging.Log;
import org.zhiqim.kernel.logging.LogFactory;
import org.zhiqim.kernel.util.Files;
import org.zhiqim.kernel.util.Randoms;
import org.zhiqim.kernel.util.Strings;
import org.zhiqim.kernel.util.Validates;

/**
 * Git服务器，配置&启动
 *
 * @version v1.0.0 @author zouzhigang 2016-10-27 新建与整理
 */
public class GitServer extends Servicer
{
    private static final Log logger = LogFactory.getLog(GitServer.class);
    
    private boolean isConfig = false;
    private String gitRootDir = null;
    private HashMapSV<Git> gitMap = null;
    
    public GitServer()
    {
        this.isConfig = true;
        this.gitMap = new HashMapSV<>();
    }

    public GitServer(String gitRootDir)
    {
        super.id = Randoms.letters(4);
        
        this.gitRootDir = gitRootDir;
        this.gitMap = new HashMapSV<>();
    }
    
    @Override
    public boolean create() throws Exception
    {
        if (isConfig)
        {//从配置中读，转化为/文件符，并去掉最后一个可能的文件符
            gitRootDir = Global.getString(id, "gitRootDir");
            gitRootDir = Files.toLinuxPath(gitRootDir);
            gitRootDir = Strings.trimRight(gitRootDir, "/");
        }
        
        if(Validates.isEmpty(gitRootDir))
        {
            logger.error("请先配置Git仓库根目录[%s.gitRootDir]", id);
            return false;
        }
        
        File gitRootFileDir = new File(gitRootDir);
        if (!gitRootFileDir.exists())
        {//不存在则创建
            Files.mkDirectory(gitRootDir);
            logger.info("创建Git仓库根目录%s", gitRootDir);
        }
        else if(!gitRootFileDir.isDirectory())
        {//存在，检查是否是目录
            logger.error("配置的Git仓库根目录[%s.gitRootDir]不正确，不是目录", id);
            return false;
        }
        
        return true;
    }
    
    /** Git根目录 */
    public String getRootDir()
    {
        return gitRootDir;
    }
    
    /** 从根目录加上相对路径 */
    public String getRootPath(String gitPath)
    {
        return gitRootDir + Strings.addStartsWith(gitPath, "/");
    }
    
    
    /******************************************************************************************/
    //Git获取&创建&删除操作
    /******************************************************************************************/
    
    /** 从缓存中获取Git对象 */
    public Git git(String gitPath)
    {
        String gitRootPath = getRootPath(gitPath);
        Git git = gitMap.get(gitRootPath);
        if (git != null)
            return git;
        
        //创建并加入到表中
        File gitDir = new File(gitRootPath);
        if (!gitDir.isDirectory())
            return null;
        
        try
        {
            git = new Git(gitDir, true);
            gitMap.put(gitRootPath, git);
            return git;
        }
        catch (IOException e)
        {
            return null;
        }
    }
    
    /** 加入Git对象到缓存 */
    public void add(String gitRootPath, Git git)
    {
        gitMap.put(gitRootPath, git);
    }
    
    /** 从缓存中移除对象 */
    public void remove(String gitRootPath)
    {
        Git git = gitMap.remove(gitRootPath);
        if (git == null)
            return;
        
        git.close();
    }
}
